package com.shigu.appVersion.service;

import com.shigu.appVersion.utils.AppVersionUtil;
import com.shigu.appVersion.vo.DownLoadResponse;
import com.shigu.appVersion.vo.SomethingResponse;
import com.shigu.appVersion.vo.VersionResponse;
import com.shigu.constant.enums.PageUri;
import com.shigu.constant.enums.VersionCtrl;
import com.shigu.constant.enums.VipConstant;
import com.shigu.constant.service.SystemConstantService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/*
 * create by  过儿  2019-06-19
 *
 */
@Service
public class VersionService {

    @Autowired
    SystemConstantService systemConstantService;

    private static final String ios = "iOS";
    private static final String android = "Android";
    private static final String real = "1";

    /**
     * @description 检查更新
     * @author 郭尔
     * @date 2019-06-20 10:01
     */
    public VersionResponse getAppUpdate(String version, String os) {

        Map<String, String> map = systemConstantService.selPageConstant(PageUri.version.toString());
        String forceUpdate = map.get(VersionCtrl.forceUpdate.toString());
        String androidUrl = map.get(VersionCtrl.androidUrl.toString());
        String iosUrl = map.get(VersionCtrl.iosUrl.toString());

        boolean isupdate = false;
        boolean force = real.equals(forceUpdate);
        String updateUrl = "";

        if (ios.equals(os)) {
            updateUrl = iosUrl;
            String iosVersion = map.get(VersionCtrl.iosVersion.toString());
            isupdate = AppVersionUtil.compareVersion(version, iosVersion) < 0;

        }
        if (android.equals(os)) {
            updateUrl = androidUrl;
            String androidVersion = map.get(VersionCtrl.androidVersion.toString());
            isupdate = AppVersionUtil.compareVersion(version, androidVersion) < 0;
        }

        VersionResponse response = new VersionResponse();
        response.setAndroidUrl(androidUrl);
        response.setIosUrl(iosUrl);
        //强更 设置为 true且版本低于更新版本时才为true
        response.setForceIs(force && isupdate);
        response.setUpdateUrl(updateUrl);
        response.setUpdateIs(isupdate);
        return response;
    }

    /**
     * @description 获取更新链接
     * @author 郭尔
     * @date 2019-06-20 10:02
     */
    public DownLoadResponse getDownloadUrl() {
        Map<String, String> map = systemConstantService.selPageConstant(PageUri.version.toString());
        String androidUrl = map.get(VersionCtrl.androidUrl.toString());
        String iosUrl = map.get(VersionCtrl.iosUrl.toString());

        return new DownLoadResponse(iosUrl, androidUrl).success();
    }

    /**
     * @description 测试ios不同字符串编码的情况
     * @author 郭尔
     * @date 2019-06-20 14:02
     */
    public SomethingResponse something(String version, String os) {
        boolean flag = true;
        if (ios.equals(os)) {
            Map<String, String> map = systemConstantService.selPageConstant(PageUri.version.toString());
            String test = map.get(VersionCtrl.testVersion.toString());
            String btn = map.get(VersionCtrl.whosyourdaddy.toString());
//            flag = real.equals(btn) || AppVersionUtil.compareVersion(version, test) != 0;
            flag = real.equals(btn) && AppVersionUtil.compareVersion(version, test) < 0;
        }
        SomethingResponse response = new SomethingResponse();
        response.setShowValue(flag);
        return response;
    }

    /**
     * @description
     * @author 郭尔
     * @date 2019-07-01 15:22
     */
    public String versionUri(String version, String os) {
        String uri = PageUri.Android.toString();
        if (ios.equals(os)) {
            Map<String, String> map = systemConstantService.selPageConstant(PageUri.version.toString());
            String test = map.get(VersionCtrl.testVersion.toString());
            String btn = map.get(VersionCtrl.whosyourdaddy.toString());

//            boolean flag = !real.equals(btn) && AppVersionUtil.compareVersion(version, test) == 0;
           boolean flag = real.equals(btn) && AppVersionUtil.compareVersion(version, test) < 0;
           uri = flag ? PageUri.iOS.toString(): PageUri.iOSTEST.toString();
        }
        return uri;
    }

}